package com.base.logistics.config;

import com.base.logistics.sf.request.SfRequestConfig;
import com.base.logistics.sf.request.SfApiRequestConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置.
 * @since 2021-03-03
 */
@Configuration
@ConditionalOnClass(SfRequestConfig.class)
@EnableConfigurationProperties(SfExpressProperties.class)
@ConditionalOnProperty(prefix = "logistics.express.sf", value = "enabled", havingValue = "true",matchIfMissing = true)
public class SfExpressAutoConfiguration {

    private final SfExpressProperties properties;

    public SfExpressAutoConfiguration(SfExpressProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean(SfRequestConfig.class)
    public SfRequestConfig sfApiRequestConfig(){
        SfConfig config = new SfConfig();
        config.setPro(properties.getPro());
        config.setCard(properties.getCard());
        config.setCode(properties.getCode());
        config.setCheck(properties.getCheck());
        config.setPrintLogo(properties.getPrintLogo());
        config.setPrintTemplateCode(properties.getPrintTemplateCode());
        config.setFileType(properties.getFileType());

        SfRequestConfig service = new SfApiRequestConfig();
        service.setConfig(config);
        return service;
    }
}